package com.pengpeng.jwt.interceptors;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pengpeng.jwt.constant.JWTConstant;
import com.pengpeng.jwt.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Describe:
 * Created by AlexP on 2021/11/18.
 */
@Slf4j
public class JWTInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头中令牌
        String token = request.getHeader(JWTConstant.TOKEN_HEADER);
        log.info("当前token为：{}",token);
        Map<String, Object> map = new HashMap<>();
        try {
            JWTUtils.verify(token.replace(JWTConstant.TOKEN_PREFIX,""));
            return true;
        }catch (SignatureVerificationException e){
            e.printStackTrace();
            map.put("msg","签名不一致");
        }catch (TokenExpiredException e){
            map.put("msg","令牌过期");
        }catch (AlgorithmMismatchException e){
            map.put("msg","算法不匹配");
        }catch (InvalidClaimException e){
            map.put("msg","失效的payload");
        }catch (Exception e){
            map.put("msg","token无效");
        }
        map.put("state",false);

        String json = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
        return false;
    }
}
